package com.zy.imagineCommunity.service;

import com.zy.imagineCommunity.dao.MessageMapper;
import com.zy.imagineCommunity.entity.FriendsText;
import com.zy.imagineCommunity.entity.Message;
import com.zy.imagineCommunity.entity.MessageText;
import com.zy.imagineCommunity.entity.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageService {

    @Autowired
    MessageMapper messageMapper;

    //增加信息
    public void addMessage(Message message) {
        messageMapper.addMessage(message);
    }

    //根据formToId、起始条数、每页个数查询信息
    public Page<MessageText> queryFriendsByParentIdAndStatusAndPageNoAndPageSize(String fromToId, int pageNo, int pageSize) {
        Page<MessageText> page = new Page<MessageText>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = messageMapper.queryMessageTotalCountByFromToId(fromToId);
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<MessageText> messageList = messageMapper.queryMessageTextByFromToIdAndBeginAndPageSize(fromToId, begin, pageSize);
        //设置每页内容
        page.setItems(messageList);
        return page;
    }

    /**
     * 根据私信id删除信息
     *
     * @param messageId
     */
    public void deleteMessageByMessageId(int messageId) {
        messageMapper.deleteMessageByMessageId(messageId);
    }

}
